#!/usr/bin/env bash

NOWDIR=$(cd "$(dirname "$0")"; pwd)
cd $NOWDIR

sudo mkdir -p /etc/caddy
sudo cp Caddyfile /etc/caddy
sudo cp *.caddy /etc/caddy

curl https://getcaddy.com | sudo bash -s personal tls.dns.dnspod,http.expires

sudo cp caddy.service /etc/systemd/system/caddy.service

# Set up the user, group, and directories that will be needed:

sudo groupadd -g 33 www-data
sudo useradd \
      -g www-data --no-user-group \
        --home-dir /var/www --no-create-home \
          --shell /usr/sbin/nologin \
            --system --uid 33 www-data

sudo mkdir /etc/caddy
sudo chown -R root:root /etc/caddy
sudo mkdir /etc/ssl/caddy
sudo chown -R root:www-data /etc/ssl/caddy
sudo chmod 0770 /etc/ssl/caddy

# Place your caddy configuration file ("Caddyfile") in the proper directory and give it appropriate ownership and permissions:

sudo chown root:root /etc/caddy/Caddyfile
sudo chmod 644 /etc/caddy/Caddyfile

# Create the home directory for the server and give it appropriate ownership and permissions:

sudo mkdir /var/www
sudo chown www-data:www-data /var/www
sudo chmod 555 /var/www

sudo systemctl daemon-reload
sudo systemctl start caddy
sudo systemctl enable caddy
